package com.butterfly.hotkey.worker.framework.netty.handler;

import cn.hutool.core.collection.CollUtil;
import com.butterfly.hotkey.core.constant.Constants;
import com.butterfly.hotkey.core.constant.MsgTypeConst;
import com.butterfly.hotkey.core.model.HotKeyModel;
import com.butterfly.hotkey.core.model.msg.HotKeyMsg;
import com.butterfly.hotkey.core.rule.ClientKeyRule;
import com.butterfly.hotkey.core.util.NettyMsgUtil;
import com.butterfly.hotkey.common.factory.CommonFactoryBean;
import com.butterfly.hotkey.common.netty.INettyMsgHandler;
import com.butterfly.hotkey.worker.framework.component.WorkerContainer;
import com.butterfly.hotkey.worker.store.HotkeyStoreService;
import com.google.common.collect.Lists;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.butterfly.hotkey.core.constant.MsgTypeConst.APP_CONNECT;
import static com.butterfly.hotkey.worker.constants.FactoryConstant.HANDLER;

/**
 * 客户端上报自己的appName
 *
 * @author butterfly
 */
@Component
@CommonFactoryBean(type = HANDLER, valueInt = APP_CONNECT)
public class AppConnectNettyMsgHandler implements INettyMsgHandler {

    private static final Logger log = LoggerFactory.getLogger(Constants.WORKER);

    @Resource
    private WorkerContainer workerContainer;

    @Resource
    private HotkeyStoreService hotkeyStoreService;

    @Override
    public void handle(HotKeyMsg message, ChannelHandlerContext context) {
        String appName = message.getAppName();

        // 保存好客户端的规则信息
        List<ClientKeyRule> clientKeyRules = message.getClientKeyRules();
        Map<String, List<ClientKeyRule>> appRuleMap = workerContainer.getAppRuleMap();
        appRuleMap.put(appName, clientKeyRules);

        // TODO 推送dashboard新的规则信息

        // 添加客户端信息
        workerContainer.addClientApp(appName, context);

        // 推送给Client，当前已有哪些hotkey
        pushClientExistHotkey(context, appName);
    }

    private void pushClientExistHotkey(ChannelHandlerContext context, String appName) {
        Iterator<List<String>> iterator = hotkeyStoreService.queryHotkeys(appName);
        while (iterator.hasNext()) {
            List<String> hotkeys = iterator.next();
            if (CollUtil.isEmpty(hotkeys)) {
                break;
            }

            Collection<HotKeyModel> hotKeyModels = Lists.newArrayList();
            HotKeyMsg hotKeyMsg = new HotKeyMsg(MsgTypeConst.RESPONSE_NEW_HOT_KEY);
            for (String hotkey : hotkeys) {
                HotKeyModel model = new HotKeyModel();
                model.setRemove(false);
                model.setKey(hotkey);
                hotKeyModels.add(model);
            }
            hotKeyMsg.setHotKeyModels(hotKeyModels);
            context.write(NettyMsgUtil.buildByteBuf(hotKeyMsg));
        }
    }
}
